package sy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.BaseDaoI;
import sy.model.Testproject;
import sy.pageModel.DataGrid;
import sy.pageModel.Project;
import sy.service.ProjectServiceI;

@Service("projectService")
public class ProjectServiceImpl implements ProjectServiceI {

	private BaseDaoI<Testproject> projectDao;

	public BaseDaoI<Testproject> getProjectDao() {
		return projectDao;
	}

	@Autowired
	public void setProjectDao(BaseDaoI<Testproject> projectDao) {
		this.projectDao = projectDao;
	}

	private void changeModel(List<Testproject> l, List<Project> nl) {
		if (l != null && l.size() > 0) {
			for (Testproject t : l) {
				Project u = new Project();
				BeanUtils.copyProperties(t, u);
				u.setStatustext(decodeStatus(u.getStatus()));	//status 转换为文本
				nl.add(u);
			}
		}
	}

	private String addOrder(Project prj, String hql) {
		if (prj.getSort() != null) {
			hql += " order by " + prj.getSort() + " " + prj.getOrder();
		}
		return hql;
	}

	private String addWhere(Project prj, String hql, Map<String, Object> params) {
		hql += " where 1=1 ";
		if (prj.getName() != null && !prj.getName().trim().equals("")) {
			hql += " and t.name like '%%" + prj.getName().trim() + "%%'";
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
		if (ids == null || "".equals(ids.trim())) {
			return;
		}
		for (String id : ids.split(",")) {
			Testproject t = projectDao.get(Testproject.class, id);
			if (t != null) {
//				projectDao.delete(t);
				//修改为：逻辑删除
				t.setStatus("9");
				projectDao.update(t);
			}
		}
	}

	@Override
	public DataGrid datagrid(Project prj) throws Exception {
		DataGrid dg = new DataGrid();
		String hql = "select new Testproject(t.id,t.name,t.description,t.status,t.createdatetime,t.modifydatetime) from Testproject t ";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(prj, hql, params);
		String totalHql = "select count(*) from Testproject t ";
		Map<String, Object> params2 = new HashMap<String, Object>();
		totalHql = addWhere(prj, totalHql, params2);
		hql = addOrder(prj, hql);
		List<Testproject> l = projectDao.find(hql, params, prj.getPage(), prj.getRows());
		List<Project> nl = new ArrayList<Project>();
		changeModel(l, nl);
		dg.setTotal(projectDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	@Override
	public Project save(Project prj) {
		Testproject t = new Testproject();
		t.setId(UUID.randomUUID().toString());
		t.setName(prj.getName());
		t.setDescription(prj.getDescription());
		t.setStatus(prj.getStatus());
		t.setCreatedatetime(new Date());
		t.setModifydatetime(new Date());
		projectDao.save(t);
		prj = getProject(t.getId());
		return prj;
	}

	@Override
	public Project getProject(String id) {
		Testproject t = projectDao.get(Testproject.class, id);
		if (t != null) {
			Project b = new Project();
			b.setId(t.getId());
			b.setName(t.getName());
			b.setStatus(t.getStatus());
			b.setStatustext(decodeStatus(t.getStatus()));
			b.setDescription(t.getDescription());
			b.setCreatedatetime(t.getCreatedatetime());
			b.setModifydatetime(t.getModifydatetime());
			return b;
		}
		return null;
	}

	@Override
	public Project edit(Project prj) {
		Testproject t = projectDao.get(Testproject.class, prj.getId());
		if (t != null) {
			t.setName(prj.getName());
			t.setDescription(prj.getDescription());
			t.setStatus(prj.getStatus());
			t.setModifydatetime(new Date());
			projectDao.save(t);
			prj = getProject(t.getId());
		}
		return prj;
	}
	
	public String decodeStatus(String status) {
		String text = "";
		switch (Integer.parseInt(status)) {
		case 0:
			text = "未启用";
			break;
		case 1:
			text = "启用";
			break;
		case 9:
			text = "已删除";
			break;
		default:
			break;
		}
		return text;
	}

}
